package com.topscore.uke.ui.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.topscore.uke.R;
import com.topscore.uke.entities.MemberTagType;
import com.topscore.uke.entities.MemberTagValue;
import com.topscore.uke.entities.MemberTagsInfo;
import com.topscore.uke.entities.TagInfoBean;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.ui.widgets.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MultiSelectTagActivity extends ToolBarCompatActivity {

    @Bind(R.id.tags_recycler)
    RecyclerView tagsRecycler;

    private MemberTagType mTagType;

//    private final String[] _TagTitles = {"纪录片", "综艺节目", "明星", "生活", "好莱坞", "财经", "中国好声音", "电视剧", "科技", "娱乐", "演唱会"};

    private BaseQuickAdapter<MemberTagValue, BaseViewHolder> mAdapter;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_confirm, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_confirm:
//                List<MemberTagValue> list = new ArrayList<>();
//                for (int i = 0; i < mAdapter.getData().size(); i++) {
//                    if (mAdapter.getData().get(i).isSelected()){
//                        list.add(mAdapter.getData().get(i));
//                    }
//                }
//                MemberTagsInfo info = new MemberTagsInfo();
//                info.setTags(list);
//                Intent intent = new Intent();
//                intent.putExtra("tags",info);
//                setResult(RESULT_OK,intent);
                Intent intent = new Intent();
                intent.putExtra("tag_type", mTagType);
                setResult(RESULT_OK,intent);
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTagType = getIntent().getParcelableExtra("tag_type");
        setContentView(R.layout.activity_multi_select_tag);
        ButterKnife.bind(this);
        setCentralTitle("请选择");
        initViews();
    }

    private void initViews() {
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this, FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.CENTER);
        tagsRecycler.setLayoutManager(layoutManager);
        tagsRecycler.addItemDecoration(new DividerGridItemDecoration(mContext, R.drawable.stuff_grid_decoration));
        mAdapter = new BaseQuickAdapter<MemberTagValue, BaseViewHolder>(R.layout.multi_tag_select_item, mTagType.getTagValues()) {
            @Override
            protected void convert(BaseViewHolder holder, MemberTagValue item) {
                TextView textView = holder.getView(R.id.item_text);
                textView.setText(item.getLabel());
                if (item.isSelected()) {
                    textView.setTextColor(Color.WHITE);
                    holder.getView(R.id.item_convert).setBackgroundResource(R.drawable.tag_selected_bg);
                } else {
                    textView.setTextColor(Color.parseColor("#999999"));
                    holder.getView(R.id.item_convert).setBackgroundResource(R.drawable.tag_unselected_bg);
                }

                holder.getConvertView().setOnClickListener(v -> {
                    if (item.isSelected()) {
                        item.setSelected(false);
                    } else {
                        item.setSelected(true);
                    }
                    notifyDataSetChanged();
                });
            }
        };

        tagsRecycler.setAdapter(mAdapter);
//        initData();
    }

    private void initData() {
//        List<TagInfoBean> tagList = new ArrayList<>();
//        for (int i = 0; i < _TagTitles.length; i++) {
//            TagInfoBean bean = new TagInfoBean();
//            bean.setId(i+1);
//            bean.setTitle(_TagTitles[i]);
//            tagList.add(bean);
//        }
//        mAdapter.setNewData(tagList);
    }
}
